package com.yc.forum.servlet;

import com.yc.forum.dao.CategoryDao;
import com.yc.forum.dao.RedisDao;
import com.yc.forum.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

/**
 * 根据帖子id来获取帖子详情和发布者信息
 */
@WebServlet(name="QueryOnePostByIdServlet",value = "/queryOnePostByIdServlet.s")
public class QueryOnePostByIdServlet extends HttpServlet {
    private CategoryDao categoryDao=new CategoryDao();
    RedisDao redisDao=new RedisDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String postid=req.getParameter("postId");
        Map<String,Object> onePost= categoryDao.select_one_postAndUser(postid);
        //判空
        if(onePost.get("likesum")==null){
            onePost.put("likesum",0);
        }
        if(onePost.get("commentsum")==null){
            onePost.put("commentsum",0);
        }
        if(onePost.get("collectsum")==null){
            onePost.put("collectsum",0);
        }
        //判断当前登录账号是否给该帖子点赞或收藏
        HttpSession session=req.getSession();
        try{
            //获取当前登录账号
            String account=session.getAttribute("loginedUser").toString();
            //判断该文章是否被当前登录账号点赞
            boolean isdianzan= redisDao.accountIsInArray(postid,account);
            onePost.put("isdianzan",isdianzan);
            //判断当前登录账号是否收藏该篇文章
            boolean iscollection=redisDao.accountIsCollectionThePost(postid,account);
            onePost.put("iscollection",iscollection);
            //将帖子id加入用户浏览记录
            redisDao.addBrowsePostId(postid,account);

        }catch(Exception e){

        }
        System.out.println("In servlet:  " + onePost.toString());
        Utils.toJson(onePost,resp);
    }
}
